import {
  request
} from '../../api/index';
import {
  fillAll
} from '../../utils/util';
import {
  FIRING,
  hasErr,
  inputValidHandle
} from '../../utils/valid'

const app = getApp();

const form = {
  firingCurve: {
    name: 'F1',
    type: '烧成温度',
    value: ''
  },
  firingTime: {
    name: 'F2',
    type: '烧成时间',
    value: '',
    err: '',
    desc: FIRING['firingTime'].unit
  },
  furnaceLength: {
    name: 'F3',
    type: '炉长度',
    value: '',
    err: '',
    desc: FIRING['furnaceLength'].unit
  },
  measuredTemperature: {
    name: 'F4',
    type: '实测温度',
    value: '',
    err: '',
    desc: FIRING['measuredTemperature'].unit
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeArea: 0,
    form,
    canNext: false,
    id: '',
    fields: Object.keys(form),
    show: false,
    dataSource: "{}",
    recordState: app.globalData.editState
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo().then(res => {
      if (res.errMsg === 'getSystemInfo:ok') {
        this.setData({
          safeArea: res.safeArea.bottom - res.safeArea.height + 'px'
        })
      }
    })
    this.setPageProperty(options);
    this.fetchData();
  },

  /**
   * 设置页面属性
   */
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '8-坯体烧成'
    })
    this.setData({
      id: options.id,
      fromNavigation: options.navigation || 0,
      recordState: app.globalData.editState
    })
  },
  fetchData() {
    request.firingList({
      collectionRecordIds: this.data.id,
    }, (e) => {
      if (e[0]) {
        let k = Object.assign({}, this.data.form);
        e[0] && this.data.fields.forEach(item => {
          k[item].value = e[0][item],
            k.id = e[0].id
        })
        this.setData({
          form: k,
          dataSource: e[0].firingCurve
        })
      }
    })
  },
  onInput(event) {
    const key = event.currentTarget.dataset.key,
      value = event.detail,
      form = this.data.form;
    const target = FIRING[key];
    if (inputValidHandle(value, FIRING, key) === false) {
      form[key]['err'] = '数值不正确：' + target.desc;
    } else {
      form[key]['err'] = '';
    }
    form[key]['value'] = String(value);
    this.setData({
      form
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  onTap(event) {
    if (event.currentTarget.dataset.key === 'firingCurve') {
      this.setData({
        show: true
      })
    }
  },
  onSubmint(event) {
    this.setData({
      form: {
        ...this.data.form,
        firingCurve: {
          ...form.firingCurve,
          value: JSON.stringify(event.detail.value)
        }
      }
    })
    this.onClose();
  },
  nextHandle() {
    if (this.data.fromNavigation > 0) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    wx.redirectTo({
      url: '../product/index?id=' + this.data.id
    })
  },
  toHome() {
    wx.navigateBack({
      delta: 7
    })
  },
  onChange(event) {
    if (hasErr(this.data.form)) {
      wx.showToast({
        title: '存在不合理的数值',
        icon: 'none'
      })
      return
    }
    fillAll(event.detail.value, true) && request.firingAdd(Object.assign({
      collectionRecordId: this.data.id
    }, this.data.formResult, event.detail.value, {
      id: this.data.form.id,
      firingCurve: this.data.form['firingCurve'].value
    }), this.nextHandle)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})